package module.scene.view.iso.base
{
	import code.obj.IconObj;
	
	import com.darwin.darwinBitmapEngine.core.Node;
	import com.darwin.darwinIsometric.core.IsoObject;
	import com.darwin.darwinIsometric.core.IsoPosition;
	import com.darwin.darwinIsometric.core.Point3D;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilter;
	import flash.utils.Timer;
	
	import module.scene.FogManager;
	import module.scene.consts.SceneConst;
	import module.scene.event.JusticeIsoObjectEvent;
	import module.scene.model.dataObj.IsoDataObj;
	import module.scene.utils.SceneUtil;

	/**
	 *	正义基础isoObject 
	 * @author Roland
	 * 
	 */
	public class JusticeIsoObject extends IsoObject
	{
		protected var _isoDataObj:IsoDataObj;
		protected var _dataObj:Object;
		protected var _serverId:String;
		private var _fieldVisibleRange:int = 0;
		private var _openFieldVisible:Boolean = false;
		private var _hideWithFog:Boolean = true;
		private var _fogUpdateTimer:Timer;
		/**
		 *	此物件是否已经被销毁
		 */
		public var disposed:Boolean = false;

		/**
		 *	如果此物件为组合部件，则此数组中包含属于此组合部件的所有iso的引用，包括自己
		 */
		public var parts:Array;
		/**
		 *	实例化一个isoObject 
		 * @param isoDataObj 场景物件描述数据
		 * @param dataObj 配置数据强类型
		 * 
		 */
		public function JusticeIsoObject(isoDataObj:IsoDataObj,dataObj:Object = null)
		{
			super(isoDataObj.size * SceneConst.GRID_SIZE, isoDataObj.size * SceneConst.GRID_SIZE);
			this.dataObj = dataObj;
			this.isoDataObj = isoDataObj;
			_fogUpdateTimer = new Timer(200);
			_fogUpdateTimer.addEventListener(TimerEvent.TIMER,fogTimerHandler);
		}
		
		private function fogTimerHandler(e:Event = null):void
		{
			updateFog();
		}
		
		public function updateFog():void
		{
			if(FogManager.instance)
			{
				var inFog:Boolean = FogManager.instance.isInFog(this);
				if(hideWithFog==true && openFieldVisible == false && inFog == true)
				{
					this.visible = false;
				}else{
					this.visible = true;
				}
				var show:Boolean = false;
				if(this.parentWorld && this.visible == true && this.openFieldVisible == true)
				{
					show = true;
				}
				FogManager.instance.updateArea(this,this.fieldVisibleRange,show);
			}
		}
		
		override public function dispose():void
		{
			super.dispose();
			parts = null;
			disposed = true;
		}
		
		/**
		 *	实体id,此物件在场景中的唯一id 
		 */
		public function get eId():String
		{
			return _isoDataObj.eId;
		}
		
		/**
		 *	此物件所属玩家id
		 */
		public function get userId():String
		{
			return _isoDataObj.userId;
		}
		
		private var _filter:BitmapFilter;
		/**
		 * 位图滤镜
		 */
		public function get filter():BitmapFilter
		{
			return _filter;
		}
		
		public function set filter(value:BitmapFilter):void
		{
			if(value != _filter)
			{
				_filter = value;
				for (var i:int = 0; i < numChildren; i++) 
				{
					(getNodeAt(i) as Node).filter = _filter;
				}
			}
		}
		
		public function set isoDataObj(value:IsoDataObj):void
		{
			_isoDataObj = value;
			_serverId = _isoDataObj.serverId;
			this.isCombo = _isoDataObj.isCombo;
			this.comboRim = _isoDataObj.comboRim;
			this.isoType = _isoDataObj.isoType;
			this.walkable = _isoDataObj.walkable;
		}
		
		public function get isoDataObj():IsoDataObj
		{
			return _isoDataObj;
		}
		
		public function get dataObj():Object
		{
			return _dataObj;
		}
		
		public function set dataObj(value:Object):void{
			_dataObj = value;
		}

		/**
		 *	此物件是否会随迷雾消失 
		 */
		public function get hideWithFog():Boolean
		{
			return _hideWithFog;
		}

		/**
		 * @private
		 */
		public function set hideWithFog(value:Boolean):void
		{
			_hideWithFog = value;
//			updateFog();
			if(_hideWithFog)
			{
				_fogUpdateTimer.start();
			}else{
				_fogUpdateTimer.stop();
			}
		}

		/**
		 *	是否打开视野，如果为true且视野范围不为0，则此物件会驱散视野范围内的迷雾
		 */
		public function get openFieldVisible():Boolean
		{
			return _openFieldVisible;
		}

		/**
		 * @private
		 */
		public function set openFieldVisible(value:Boolean):void
		{
			_openFieldVisible = value;
			updateFog();
		}

		/**
		 *	视野范围,如果不为0,则此物件具有驱散迷雾功能，驱散范围与fieldVisibleRange值成正比
		 */
		public function get fieldVisibleRange():int
		{
			return _fieldVisibleRange;
		}

		/**
		 * @private
		 */
		public function set fieldVisibleRange(value:int):void
		{
			_fieldVisibleRange = value;
			updateFog();
		}
		
		override public function set x3d(value:Number):void
		{
			super.x3d = value;
			updateFog();
		}
		
		override public function set z3d(value:Number):void
		{
			super.z3d = value;
			updateFog();
		}
		
		/**
		 *	获取当前isoObject在整个大世界地图中的绝对坐标 (基于格子),此坐标通常被用来发送给后台
		 * @return 
		 * 
		 */
		public function get absIsoPosition():IsoPosition
		{
			var pos:IsoPosition = SceneUtil.relPosToAbsPos(x3d/SceneConst.GRID_SIZE,z3d/SceneConst.GRID_SIZE);
			return pos;
		}

		override public function dispatchEvent(event:Event):Boolean
		{
			var result:Boolean = super.dispatchEvent(event);
			var $isoEvent:JusticeIsoObjectEvent = new JusticeIsoObjectEvent(JusticeIsoObjectEvent.ISO_DISPATCH_EVENT);
			$isoEvent.data = event;
			super.dispatchEvent($isoEvent);
			return result;
		}


	}
}